$(function() {
	$('#search_user_level').live('click', function() {
		adminUserLevel.searchUserLevel('page');
	});
	
	$('#export_user_level').live('click', function() {
		adminUserLevel.searchUserLevel('csv');
	});
	
	$('#attr_id').live('change', function() {
		adminUserLevel.getLbyAttrId();
	});
	
	$('#attr_L_id').change(function(){
		adminUserLevel.checkBrand();
	});
	$('#attr_M_id').change(function(){
		adminUserLevel.checkBrand();
	});
});

var adminUserLevel = (function(){

	var searchUserLevel = function(type) {
		var s_username = $('#s_username').val();
		var s_department = $('#s_department').val();
		var attr_id = $('#attr_id').val();
		var L = $('#attr_L_id').val();
		var M = $('#attr_M_id').val();
		var B = $('#attr_B_id').val();
		var url = _siteUrl+'admincp/userPositionLevel/index?search[s_username]='+encodeURI(s_username)+'&search[s_department]='+s_department+'&attr_id='+attr_id+'&search[L]='+L+'&search[M]='+M+'&search[B]='+B+'&search[type]='+type;
		if(type=='csv') {
			window.open(url); 	
		} else if(type=='page'){
			document.location.href = url;
		}
	}
	
	var getLbyAttrId = function() {
		var attr_id = $("#attr_id").val();
		$.ajax({
			type: "GET",
			url: _siteUrl+"admincp/hrPositionLevel/GetLByAttrId/",
			data: "attr_id="+attr_id,
			dataType: "JSON",
			success: function(data){
				adminUserLevel.createOption(data);
			}
		});	
	}
	
	var createOption = function(d) {
		html = '<option value="0">请选择</option>';
		if(d)
		{
			$.each(d,function(){
				//alert(this.id);
				html += '<option value="'+this.id+'">'+this.name+'</option>';
			});
		}
		$("#attr_L_id").html(html);
	}
	
	var updateLevel = function() {
		var unique_id = $("#user_level_unique_id").val();
		var B = $("#B").val();
		var attr_M_id = $("#attr_M_id").val();
		var attr_L_id = $("#attr_L_id").val();
		var attr_id = $("#attr_id").val();
		var title = $("#title").val();
		var title_en = $("#title_en").val();
		$.ajax({
			type: "POST",
			url: _siteUrl+"/admincp/HecUser/updateUserLevel/",
			data: "unique_id="+unique_id+"&attr_M_id="+attr_M_id+"&B="+B+"&attr_L_id="+attr_L_id+"&attr_id="+attr_id+"&title="+title+"&title_en="+title_en+"&YII_CSRF_TOKEN="+$("#YII_CSRF_TOKEN2").val(),
			success: function(msg){
				if(msg == 0) {
					alert("修改成功！");
					window.location.href = (_siteUrl+"/admincp/userPositionLevel/index");
				}
			}
		});
	}
	
	var createLevel = function() {
		var unique_id = $("#user_level_unique_id").val();
		if(unique_id == '') {
			alert("请填写工号！");
			return false;	
		}
		var B = $("#B").val();
		var attr_M_id = $("#attr_M_id").val();
		var attr_L_id = $("#attr_L_id").val();
		var attr_id = $("#attr_id").val();
		var title = $("#title").val();
		var title_en = $("#title_en").val();
		$.ajax({
			type: "POST",
			url: _siteUrl+"/admincp/HecUser/createUserLevel/",
			data: "unique_id="+unique_id+"&attr_M_id="+attr_M_id+"&B="+B+"&attr_L_id="+attr_L_id+"&attr_id="+attr_id+"&title="+title+"&title_en="+title_en+"&YII_CSRF_TOKEN="+$("#YII_CSRF_TOKEN2").val(),
			success: function(msg){
				if(msg == 0) {
					alert("添加成功！");
					window.location.href = (_siteUrl+"/admincp/userPositionLevel/index");
				}
			}
		});
	}
	
	var checkBrand = function() {
		//var unique_id = $("#user_level_unique_id").val();
		//var B = $("#B").val();
		$("#max_b").html();
		var attr_M_id = $("#attr_M_id").val();
		var attr_L_id = $("#attr_L_id").val();
		$.ajax({
			type: "GET",
			url: _siteUrl+"/admincp/UserPositionLevel/checkBrand/",
			data: "attr_M_id="+attr_M_id+"&attr_L_id="+attr_L_id,
			//dataType: 'json',
			success: function(msg){
				if(msg != 0) {
					$("#max_b").html(msg);
					$("#B").val(msg);
				}
			}
		});
	}
	
	return {
		searchUserLevel : searchUserLevel,
		getLbyAttrId : getLbyAttrId,
		createOption : createOption,
		updateLevel : updateLevel,
		createLevel : createLevel,
		checkBrand : checkBrand
	};
	
})();